let App = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataType: 'all',
    list: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.dataType = options.type || '';
    this.setData({ dataType: this.data.dataType });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // 获取订单列表
    this.getOrderList(this.data.dataType);
  },

  /**
   * 获取订单列表
   */
  getOrderList: function (dataType) {
    let _this = this;
    App._get('user.order/lists', { "dataType": "all", "is_tuan": "1","isgroupend":dataType }, function (result) {
      _this.setData(result.data);
      result.data.list.length && wx.pageScrollTo({
        scrollTop: 0
      });

      _this.getTime();
    });
  },
  getTime: function () {
    var that = this;

    function nowTime() {//时间函数
      // console.log(a)
      var dates = that.data.list;
      for (var i = 0; i < dates.length; i++) {
        console.log("dates[i].grouendtime", dates[i].grouendtime);
        var etime = new Date(dates[i].grouendtime).getTime();

        var stime = new Date().getTime();
        console.log("ss", stime, etime);
        var usedTime = etime - stime;

        dates[i].dat = usedTime / 1000;
      }
      //console.log("dates", dates.length);
      for (var i = 0; i < dates.length; i++) {

        var intDiff = dates[i].dat;//获取数据中的时间戳
        console.log("intDiff", intDiff)
        var day = 0, hour = 0, minute = 0, second = 0;
        if (intDiff > 0) {//转换时间
          day = Math.floor(intDiff / (60 * 60 * 24));
          hour = Math.floor(intDiff / (60 * 60)) - (day * 24);
          minute = Math.floor(intDiff / 60) - (day * 24 * 60) - (hour * 60);
          second = Math.floor(intDiff) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60);
          if (hour <= 9) hour = '0' + hour;
          if (minute <= 9) minute = '0' + minute;
          if (second <= 9) second = '0' + second;
          dates[i].dat = dates[i].dat - 1;
          var str = hour + ':' + minute + ':' + second
          console.log(str)
        } else {
          var str = "已结束！";
          clearInterval(timer);
        }
        // console.log(str);
        dates[i].endtimestr = str;//在数据中添加difftime参数名，把时间放进去
      }
      that.setData({
        list: dates
      })
      // console.log(that)
    }

    nowTime();
    var timer = setInterval(nowTime, 1000);
  },
  /**
   * 切换标签
   */
  bindHeaderTap: function (e) {
    this.setData({ dataType: e.target.dataset.type });
    // 获取订单列表
    this.getOrderList(e.target.dataset.type);
  },

  /**
   * 取消订单
   */
  cancelOrder: function (e) {
    let _this = this;
    let order_id = e.currentTarget.dataset.id;
    wx.showModal({
      title: "提示",
      content: "确认取消订单？",
      success: function (o) {
        if (o.confirm) {
          App._post_form('user.order/cancel', { order_id }, function (result) {
            _this.getOrderList(_this.data.dataType);
          });
        }
      }
    });
  },

  /**
   * 确认收货
   */
  receipt: function (e) {
    let _this = this;
    let order_id = e.currentTarget.dataset.id;
    wx.showModal({
      title: "提示",
      content: "确认收到商品？",
      success: function (o) {
        if (o.confirm) {
          App._post_form('user.order/receipt', { order_id }, function (result) {
            _this.getOrderList(_this.data.dataType);
          });
        }
      }
    });
  },

  /**
   * 发起付款
   */
  payOrder: function (e) {
    let _this = this;
    let order_id = e.currentTarget.dataset.id;

    // 显示loading
    wx.showLoading({ title: '正在处理...', });
    App._post_form('user.order/pay', { order_id }, function (result) {
      if (result.code === -10) {
        App.showError(result.msg);
        return false;
      }
      // 发起微信支付
      // console.log("timeStamp", result.data.timeStamp);
      // console.log("nonceStr", result.data.nonceStr);
      // console.log("prepay_id", result.data.prepay_id);
      // console.log("timeStamp", result.data.timeStamp);
      console.log("paySign", result.data.paySign);

      wx.requestPayment({
        timeStamp: result.data.timeStamp,
        nonceStr: result.data.nonceStr,
        package: 'prepay_id=' + result.data.prepay_id,
        signType: "RSA",
        paySign: result.data.paySign,
        success: function (res) {
          App.showError('支付成功', function () {

          });
        },
        fail: function () {
          App.showError('订单未支付');
        },
      });
    });
  },

  /**
   * 跳转订单详情页
   */
  detail: function (e) {
    let order_id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../order/groupdetail?order_id=' + order_id
    });
  },

  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
  }


});